package com.project.m.api.busi.handlers.hicatcard.merchant;

import com.project.m.api.busi.bean.merchant.MerchantTransactionReturnReq;
import com.project.m.api.busi.bean.merchant.MerchantTransactionReturnResp;
import com.project.m.api.busi.config.BusiError;
import com.project.m.api.busi.db.entity.hicatcard.transaction.TTransaction;
import com.project.m.api.busi.db.service.hicatcard.transaction.TransactionService;
import com.project.m.api.busi.handlers.MerchantBizParamChecker;
import com.project.m.api.common.biz.BizHandler;
import com.project.m.api.validates.ValidateParam;
import com.project.m.api.validates.ValidateRet;
import com.project.m.api.validates.ValidateType;
import com.project.m.api.validates.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MerchantTransactionReturnBizHandler
		extends MerchantBizParamChecker<MerchantTransactionReturnReq, MerchantTransactionReturnResp>
		implements BizHandler<MerchantTransactionReturnReq, MerchantTransactionReturnResp> {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	TransactionService transactionService;

	@Override
	public MerchantTransactionReturnResp handle(MerchantTransactionReturnReq bizRequest) throws Exception {
		// TODO Auto-generated method stub
		MerchantTransactionReturnResp bizResp = new MerchantTransactionReturnResp();
		// 参数检查
		ValidateRet vRet = checkParam(bizRequest, bizResp);
		if (!vRet.isValid()) {
			return bizResp;
		}

		// 判断merchantId + transactionorder
		try {

			Long merchantId = Long.parseLong(bizRequest.getMerchant_id());
			TTransaction transaction = transactionService.findByMerchantIdAndTransactionOrder(merchantId,
					bizRequest.getTransaction_order());
			if (transaction == null) {
				bizResp.setResult_code(BusiError.ERR_CODE_ORDER_NONE);
				bizResp.setError_message(BusiError.ERR_MSG_ORDER_NONE);
				return bizResp;
			}
			
			if(transaction.getStatus().equals(TTransaction.STATUS_SETTLE)) {
				bizResp.setResult_code(BusiError.ERR_CODE_MERCHANT_SERVICE_HAS_SETTLE);
				bizResp.setError_message(BusiError.ERR_MSG_MERCHANT_SERVICE_HAS_SETTLE);
				return bizResp;
			}
			
			transactionService.transactionReturn(transaction);

			bizResp.setResult_code(BusiError.ERR_CODE_SUCCESS);
			bizResp.setError_message("");
			return bizResp;
		} catch (Exception e) {
			logger.error("内部出错", e);
			bizResp.setResult_code(BusiError.ERR_CODE_DB_CONF);
			bizResp.setError_message(BusiError.ERR_MSG_DB_CONF);
			return bizResp;
		}

	}

	@Override
	public ValidateRet checkParam(MerchantTransactionReturnReq req, MerchantTransactionReturnResp resp) {
		ValidateRet vRet = super.checkParam(req, resp);
		if (!vRet.isValid())
			return vRet;
		List<ValidateParam> paramList = new ArrayList<ValidateParam>();
		paramList.add(new ValidateParam("merchant_id", ValidateType.NOTBLANK, req.getMerchant_id()));
		paramList.add(new ValidateParam("transaction_order", ValidateType.NOTBLANK, req.getTransaction_order()));

		vRet = ValidateUtil.validate(paramList);
		if (vRet == null) {
			vRet = new ValidateRet(true, "");
		}

		if (!vRet.isValid()) {
			resp.setResult_code(BusiError.ERR_CODE_PARAM_BAD_REQ);
			resp.setError_message(vRet.getErrMsg());
		}

		return vRet;
	}
}
